package com.happyhome.services;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.happyhome.constants.ComTxManagerName;
import com.happyhome.dao.HouseDao;
import com.happyhome.datamodel.District;
import com.happyhome.datamodel.House;
import com.happyhome.datamodel.User;

@Service
public class HouseServiceImpl implements HouseService {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(HouseServiceImpl.class);

	@Autowired
	private HouseDao houseDao;

	@Override
	public House findHouseById(Long id) {
		House house = houseDao.find(id);
		return house;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public House merge(House house) {
		return houseDao.merge(house);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public List<House> getAll() {
		List<House> listResult = houseDao.findAll();
		for(House house : listResult){
			house.setManagerName(house.getUser().getFullName());
		}
		return listResult;
	}
	
	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public House findEagerHouseById(Long id) {
		House house = houseDao.find(id);
		house.getDistrict();
		house.getDistrict().getProvince();
		return house;
	}

	@Override
	public House getHouseByCode(String codeHouse) {
		House house = houseDao.getHouseByCode(codeHouse);
		return house;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void setDisplayCavas(List<Long> listHousesId) {
		List<House> listNewsInList = houseDao.getListHousesInListId(listHousesId);
		for(House houseIn : listNewsInList){
			houseIn.setIsDisplay(Boolean.valueOf("true"));
		}
		
		List<House> listNewsOutList = houseDao.getListHousesNotInListId(listHousesId);
		for(House houseIn : listNewsOutList){
			houseIn.setIsDisplay(Boolean.valueOf("false"));
		}
		houseDao.flush();
	}

	@Override
	public List<House> getListHousesIsDisplay() {
		return houseDao.getListHousesIsDisplay();
	}

	@Override
	public List<House> getListHousesNewestLimit(Long limit) {
		return houseDao.getListHousesNewestLimit(limit);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public User getManagerInformation(House house) {
		User user = house.getUser();
		user.setFullName(user.getFullName());
		user.setMobile(user.getMobile());
		return user;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public District getDistrictByHouse(House house) {
		House houseA = houseDao.find(house.getIdHouse());
		District district= houseA.getDistrict();
		district.setNameDistrict(district.getNameDistrict());
		return district;
	}

	@Override
	public String getGoogleMap(Long idRoom) {
		return houseDao.getGoogleMap(idRoom);
	}
}
